# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=xplico
pkgver=1.2.2
pkgrel=2
epoch=1
arch=('x86_64' 'aarch64')
pkgdesc='Internet Traffic Decoder. Network Forensic Analysis Tool (NFAT).'
groups=('blackarch' 'blackarch-forensic' 'blackarch-networking')
url='https://github.com/xplico/xplico/'
license=('CC-BY-3.0' 'GPL-2.0-or-later')
depends=('tcpdump' 'libpcap' 'sqlite3' 'sox' 'lame' 'apache' 'php' 'net-tools'
         'php-sqlite' 'php-apache' 'php-pear' 'libnet' 'recode' 'json-c'
         'python-httplib2' 'python-psycopg2' 'libmariadbclient' 'ndpi' 'geoip'
         'libpqxx' 'perl' 'libmaxminddb')
optdepends=('geoip-database-extra: City database for geoip IP location lookups'
            'ghostpdl: reconstruct document printed with network printer'
            'videosnarf: decode VoIP based on RTP')
makedepends=('git' 'dpkg')
source=("https://github.com/xplico/xplico/archive/v.$pkgver.tar.gz"
        "Adapt-code-for-ndpi-4.x.patch"
        "change-default-config-file.patch"
        "add-more-explicit-message.patch"
        "change-path-to-ndpi.patch"
        "cake-Fix-argument-order-for-implode-method.patch"
        "Don-t-include-private-json-c-header.patch"
        "cake-Fix-deprecated-argument-order-in-calls-to-implode.patch"
        "Fix-Makefile-to-keep-hardening-flags-injected-by-dpkg.patch"
        "cake-Fix-failures-on-PHP-7.4.patch"
        "fix-spelling-errors.patch"
        "cake-Fix-invalid-data-type.patch"
        "Fix-upstream-makefile-to-not-fudge-with-the-debian-dir.patch"
        "cake-Fix-SchemaShell-configuration-change.patch"
        "Improve-systemd-service-file.patch"
        "cake-Fix-shutdown-warnings-in-PHP7.2.patch"
        "cake-Make-TestShell-compatible-with-PHP-7.4.patch"
        "search-php.ini-in-correct-location.patch"
        "update-for-ndpi-3.x.patch"
        "cake-php-7.4-Replace-deprecated-curly-brace-offset.patch"
        "cake-php-8-Fix-error-unknown-named-parameter-subject.patch"
        "fix-bool-typedef.patch"
        "ndpi4-support.patch"
        "fix-sql.patch")
sha512sums=('e189e39e1d76af691c4bc86ad7db3493f6341fec9aba951cf02558e06c4963b73806df06303b6ba9b6352e4a204aac5920690d61338d4d1f680257211d62a6d3'
            'c64298f9cc125e520261daccbc6a2311972d95dd2a739185ee21f26bdb64cf2cf1e1b56acded02c30e012f1efceedc903d4cabdea88990b45921231f4de012f6'
            'bad018119ee43de4f522f9e2e8151967164dbae0a911162851a7c6fcc6588d3a6bdfac3ccbe7a7c5e27cecc954ce0f0aae2188e7895e991b1566fb5c4af88a95'
            '39bd64ce17a371e8d3cdef03803ae6c41a72ded326f50f2832e88b945342dd3304a10805f737867709e337aa8fca4b436c5e9e5e8249d6583ea0e409b9c9f3fd'
            '3303d1319c05a7e80d553dbec7c1f2e178ecf93623500f025fa0fe20ad3926ea31257fb286356f52f5303532d335ffce7da19bd12e6c8b9cf21651f2ea62201d'
            '56abbc8996d90458a19b513b74f9e73d7da7b2e6711808238f21509d7961af7a6a979442949265e26fc670f9d6c2351d1e96c15a91c55340e7cebc106ea9c54c'
            'd3f625cf0ae43e0ba93009b48b16380e08b62875b813467d819746ee6855d798360e3dbf99f535d6478bbc3fc06122f41e744fe7459a2c3b13bedbaac64eb261'
            '4ba4de63c6083f7aa965015a652acc4dbee0e3c70c883e04515d25d488dad3cf96714199bc814261966c848eaf97eed06d4e00cc42ffd1a6722222e76de6375a'
            '9e2a325b25ac2a7479b40aa9da46e3bbb649d4de00d4d8e66b74172d5bf8dff09cda16bf7841a38b7620bdfa570b51ef1ef957a66ed491d6953bcd1b343b43f3'
            '48b6401b729cb33f02c5695f3e5da07bcd6b6f24e1ce50e6ff6966dfa1faf3c5621fbe43ce49827e560921ae2cba5761cd0b74cd2aac571023c84d9ac87fec22'
            '84c67ff6e7088df22d5f30dd1db011c16367f962f50b76dc87f232127af6f997aca907faac329e77ad9aa38c0847fe9b85877eca14d686fa8bdb8a64d08e7ff0'
            'a5b20923b29c3f95d2ebc0dbcd012ba5b2aff8cdc44606051dd7857884637759381ad341d70d6fb910fb3010f463b99fc43678a914b416979d24eaac1f319849'
            '379a9c3aec49ced02cb029f2fb94c7a4d6ea21cad5872dc7e6cc8bcfaf20b4b9eb1c94d5e8a1692ccc77dcbd7ea43e2b8e8370bcab7cfa608dc0ff30f7f9ee98'
            '542b62c62b5b9cb22e1331043f25d20dc1800e575090409bb6bcaa5e1bc16ed4f75d840cfcfd692ac05763d2ee06380c672f29263e389b34597cfef6d630e055'
            'a004a2dd43e11b70f56410bb3e521ab7e0a8556e96010059c41c1ef1481178fc9a9d3977f8a2365e453434d26a2be014e3357c02eb3de85fe98a5fb930fcb9aa'
            'e510bf686905caaf91d17b32be557ab44330834e10369b3f09e6f65ff3f384ed02f737092f7ce9dd1567d536f955e9fb14e145d8bede90fb8f5fab801b5e208b'
            'b8689e95c2c036891633d13e3e1c755e6ed275724fcca0846b0f9832d863024bec08b29807576bac9e2a9b6382e1beb7711239657845df4b0ff3362725b8bb22'
            'e3a01709792751e622d45768d4aa5af318799a2f58a37618ce603d9319222b3f85445d4ee20051adce3cb3971237b7a2f10ce0874272e084d99d19684469e3bb'
            'fa17a393bdcdb15296524d026f67c478c30c48f41f0fbb314514d9d003b2d2ddc6c18c0b79cb82a19dc8dd0730399ef4de95c7b10c5e41ceac671bed9e7c9203'
            '3d4edab9dc9a60fa3daf3b2be92e0ce35879453475684bf8d35345b9b57b77b7c24f02463a8a0f0ac3ac58963500c5151e56d082a4f7136bb940560f264ce7c3'
            '573129b06f6d86261b91d4917820c37450c1086ec9aa04a213e20bcb7732ad30197a69640f805d1e777e1ea1c5a65de273a1490303bb5dc730a7930abf192773'
            'f887cf34b7346489bcbefada3172e0a18146589f1a9f93d05ed41d9d91cb11c266cb83e7c069cfb00718d88fab78536804025cda92b008bd230b9e6a0ca6cda5'
            '21dd363c72c0eca8d210942c7b3bd2160fb0af9fe7d2b31a504fced0433295c2eba3ddf9c349ed5317949c0f237ac894cde2fc691cf731096dae40723ae70304'
            '9b77b896f42fdb9279cbe91bad4b1798a8eb2c66f322693fa29278fe254d7b4969b048c34ae244ae053528b3122f382bfc2e97cf0041981f971fd6d62c706492')
install="$pkgname.install"

prepare() {
  cd "$pkgname-v.$pkgver"

  patch -p1 < ../Adapt-code-for-ndpi-4.x.patch
  patch -p1 < ../change-default-config-file.patch
  patch -p1 < ../add-more-explicit-message.patch
  patch -p1 < ../change-path-to-ndpi.patch
  patch -p1 < ../cake-Fix-argument-order-for-implode-method.patch
  patch -p1 < ../Don-t-include-private-json-c-header.patch
  patch -p1 < ../cake-Fix-deprecated-argument-order-in-calls-to-implode.patch
  patch -p1 < ../Fix-Makefile-to-keep-hardening-flags-injected-by-dpkg.patch
  patch -p1 < ../cake-Fix-failures-on-PHP-7.4.patch
  patch -p1 < ../fix-spelling-errors.patch
  patch -p1 < ../cake-Fix-invalid-data-type.patch
  patch -p1 < ../Fix-upstream-makefile-to-not-fudge-with-the-debian-dir.patch
  patch -p1 < ../cake-Fix-SchemaShell-configuration-change.patch
  patch -p1 < ../Improve-systemd-service-file.patch
  patch -p1 < ../cake-Fix-shutdown-warnings-in-PHP7.2.patch
  patch -p1 < ../cake-Make-TestShell-compatible-with-PHP-7.4.patch
  patch -p1 < ../search-php.ini-in-correct-location.patch
  patch -p1 < ../update-for-ndpi-3.x.patch
  patch -p1 < ../cake-php-7.4-Replace-deprecated-curly-brace-offset.patch
  patch -p1 < ../cake-php-8-Fix-error-unknown-named-parameter-subject.patch
  patch -p1 < ../fix-bool-typedef.patch
  patch -p1 < ../ndpi4-support.patch
  patch -p1 < ../fix-sql.patch

  sed -i 's/\bstruct[[:space:]]\+pcappkt_hdr\b/struct pcap_pkthdr/g' \
    capt_dissectors/rltm_pol/rltm_pol.c \
    capt_dissectors/rltm/rltm.c

  sed -i 's/\bpckt_header\.tv_sec\b/pckt_header.ts.tv_sec/g' \
    capt_dissectors/rltm_pol/rltm_pol.c

  sed -i 's/\bpckt_header\.tv_usec\b/pckt_header.ts.tv_usec/g' \
    capt_dissectors/rltm_pol/rltm_pol.c
}

build() {
  cd "$pkgname-v.$pkgver"

  make -j1
}

package() {
  cd "$pkgname-v.$pkgver"

  _tools="dema mfbc mfile mpaltalk msite mwebymsg mwmail trigcap wbm_aol.py"
  _tools="$_tools wbm_aol_v2.py wbm_gmail.py wbm_libero.py wbm_live.py"
  _tools="$_tools wbm_rediff.py wbm_rossoalice.py wbm_yahoo.py"
  _tools="$_tools wbm_yahoo_android.py wbm_yahoo_v2.py xplico"

  mkdir -p "$pkgdir/usr/bin"

  make DESTDIR="$pkgdir" install

  ln -sr /usr/share/GeoIP/GeoIPv6.dat "$pkgdir/opt/$pkgname/GeoLiteCity.dat"
  ln -sr /usr/share/GeoIP/GeoIP "$pkgdir/opt/$pkgname/GeoLiteCityv6.dat"
  #ln -sr /usr/share/GeoIP/GeoIPCity.dat "$pkgdir/opt/xplico/GeoLiteCity.dat"

  chmod +x "$pkgdir/opt/"{xplico,xplico/cfg}

  # help user a bit
  for t in $_tools ; do
    ln -sf "/opt/$pkgname/bin/$t" "$pkgdir/usr/bin/$t"
  done
}

